import type { IOnCompleted, IOnData, IOnError, IOnFile, IOnMessageEnd, IOnMessageReplace, IOnNodeFinished, IOnNodeStarted, IOnThought, IOnWorkflowFinished, IOnWorkflowStarted } from './base'
import { get, post, ssePost,del } from './base'
import type { Feedbacktype } from '@/types/app'

// 发送聊天消息
export const sendChatMessage = async (
  body: Record<string, any>,
  {
    onData,
    onCompleted,
    onThought,
    onFile,
    onError,
    getAbortController,
    onMessageEnd,
    onMessageReplace,
    onWorkflowStarted,
    onNodeStarted,
    onNodeFinished,
    onWorkflowFinished,
  }: {
    onData: IOnData
    onCompleted: IOnCompleted
    onFile: IOnFile
    onThought: IOnThought
    onMessageEnd: IOnMessageEnd
    onMessageReplace: IOnMessageReplace
    onError: IOnError
    getAbortController?: (abortController: AbortController) => void
    onWorkflowStarted: IOnWorkflowStarted
    onNodeStarted: IOnNodeStarted
    onNodeFinished: IOnNodeFinished
    onWorkflowFinished: IOnWorkflowFinished
  },
) => {
  return ssePost('chat-messages', {
    body: {
      ...body,
      response_mode: 'streaming',
    },
  }, { onData, onCompleted, onThought, onFile, onError, getAbortController, onMessageEnd, onMessageReplace, onNodeStarted, onWorkflowStarted, onWorkflowFinished, onNodeFinished })
}
// 获取对话列表
export const fetchConversations = async () => {
  return get('conversations', { params: { limit: 100, pinned: false, user: 'abc-123' } })
}
// 获取聊天列表
export const fetchChatList = async (conversationId: string) => {
  return get('messages', { params: { conversation_id: conversationId, limit: 20, last_id: '', user: 'abc-123' } })
}

// init value. wait for server update
export const fetchAppParams = async () => {
  return get('parameters')
}

// 生成对话名称
export const generationConversationName = async (id: string) => {
  return post(`conversations/${id}/name`, { body: { auto_generate: true } })
}
// // 获取token
// export const getToken = async () => {
//   return get(`passport`, { headers: { 'x-app-code': 'ACO44KBHE8m5DGwK' } })
// }
// 停止聊天
export const stopChatMessage = async (conversationId: string) => {
  return get(`chat-messages/${conversationId}/stop`, { params: { user: 'abc-123' } })
}
// 更新反馈
export const updateFeedback = async (messageId: string, feedback: Feedbacktype) => {
  return post(`messages/${messageId}/feedbacks`, { body: { ...feedback, user: 'abc-123' } })
}


// 获取建议问题
export const getSuggestedQuestions = async (messageId: string) => {
  return get(`messages/${messageId}/suggested-questions`, { params: { user: 'abc-123' } })
}


// 更新对话名称
export const updateConversationName = async (conversationId: string, name: string) => {
  return post(`conversations/${conversationId}/name`, { body: { name, user: 'abc-123' } })
}


// 删除对话
export const deleteConversation = async (conversationId: string) => {
  return del(`conversations/${conversationId}`, { body: {  user: 'abc-123' } })
}


